import Vue from 'vue';

Vue.filter('timeFilter', (t) => {
    const dataArray = t.split(" ")[0].split("-");
    return `${dataArray[0]}年${dataArray[1]}月${dataArray[2]}日 ${t.split(" ")[1]}`
});
Vue.filter('timeStampToDay', (t) => {
    if (t) {
        const time = new Date(parseInt(t * 1000));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const d = time.getDate();
        return `${y}年${m}月${d}日`
    }
    return t;
});
Vue.filter('timeStampToDay2', (t) => {
    if (t) {
        const time = new Date(parseInt(t * 1000));
        const y = time.getFullYear();
        let m = time.getMonth() + 1;
        if (m < 10) {
            m = "0" + m;
        }
        let d = time.getDate();
        if (d < 10) {
            d = "0" + d;
        }
        return `${y}-${m}-${d}`
    }
    return t;
});
Vue.filter('decodeHtml', (s) => {
    const HTML_DECODE = {
        "&lt;": "<",
        "&gt;": ">",
        "&amp;": "&",
        "&nbsp;": " ",
        "&quot;": "\"",
        "&copy;": ""

    };
    const REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;
    s = (s !== undefined) ? s : '非法字符串';
    return (typeof s != "string") ? s :
        s.replace(REGX_HTML_DECODE,
            function ($0, $1) {
                var c = HTML_DECODE[$0];
                if (c === undefined) {
                    // Maybe is Entity Number
                    if (!isNaN($1)) {
                        c = String.fromCharCode(($1 === 160) ? 32 : $1);
                    } else {
                        c = $0;
                    }
                }
                return c;
            });
});

Vue.filter('timeStamp', (t) => {
    const time = new Date(parseInt(t) * 1000);
    const y = time.getFullYear();
    let m = time.getMonth() + 1;
    let d = time.getDate();
    let h = time.getHours();
    let mm = time.getMinutes();
    let s = time.getSeconds();
    m = m < 10 ? `0${m}` : m;
    d = d < 10 ? `0${d}` : d;
    h = h < 10 ? `0${h}` : h;
    mm = mm < 10 ? `0${mm}` : mm;
    s = s < 10 ? `0${s}` : s;
    return `${y}-${m}-${d} ${h}:${mm}:${s}`;
});

Vue.filter('NowTimeStamp', (t) => {
    const time = t;
    const y = time.getFullYear();
    let m = time.getMonth() + 1;
    let d = time.getDate();
    let h = time.getHours();
    let mm = time.getMinutes();
    let s = time.getSeconds();
    m = m < 10 ? `0${m}` : m;
    d = d < 10 ? `0${d}` : d;
    h = h < 10 ? `0${h}` : h;
    mm = mm < 10 ? `0${mm}` : mm;
    return `${y}.${m}.${d} ${h}:${mm}`;
});
Vue.filter('timeSplitDay', (t) => {
    const dataArray = t.split(" ")[0].split("-");
    return `${dataArray[1]}-${dataArray[2]} `
});
Vue.filter('timeSplitMinute', (t) => {
    const dataArray = t.split(" ")[1].split(":");
    return `${dataArray[0]}:${dataArray[1]} `
});

Vue.filter('firstLetter', (t) => {
    return t.substring(0, 1)
});


Vue.filter('nicknameHandleToStar', (t) => {
    if (t === '') {
        return '匿名用户'
    }
    const strArray = t.split('');
    let newStr = [];
    if (strArray.length < 2) {
        newStr = strArray;
        newStr[0] = '*'
    } else {
        let starNum = strArray.length - 2;
        starNum = starNum > 3 ? 3 : starNum;
        let starArray = [];
        for (let i = 0; i < starNum; i++) {
            starArray.push('*');
        }
        newStr = [strArray[0], ...starArray, strArray[strArray.length - 1]]
    }
    return newStr.join('');
});

Vue.filter('dateDiff', (t) => {
    let publishTime = Date.parse(t.replace(/-/gi, "/")) / 1000;
    let dSeconds, dMinutes, dHours, dDays, d;
    let nowTime = parseInt(new Date().getTime() / 1000);
    let date = new Date(publishTime * 1000);
    let Y = date.getFullYear();
    let M = date.getMonth() + 1;
    let D = date.getDate();
    let H = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    if (M < 10) {
        M = '0' + M;
    }
    if (D < 10) {
        D = '0' + D;
    }
    if (H < 10) {
        H = '0' + H;
    }
    if (m < 10) {
        m = '0' + m;
    }
    if (s < 10) {
        s = '0' + s;
    }
    d = nowTime - publishTime;
    dDays = parseInt(d / 86400);
    dHours = parseInt(d / 3600);
    dMinutes = parseInt(d / 60);
    dSeconds = parseInt(d);
    if (dDays > 0 && dDays < 3) {
        return dDays + '天前';
    } else if (dDays <= 0 && dHours > 0) {
        return dHours + '小时前';
    } else if (dHours <= 0 && dMinutes > 0) {
        return dMinutes + '分钟前';
    } else if (dSeconds < 60) {
        if (dSeconds <= 0) {
            return '刚刚';
        } else {
            return dSeconds + '秒前';
        }
    } else {
        return `${Y}-${M}-${D}`;
    }
});

Vue.filter('dateDiffSpecial', (t) => {
    let publishTime = Date.parse(t.replace(/-/gi, "/")) / 1000;
    let dSeconds, dMinutes, dHours, dDays, d;
    let nowTime = parseInt(new Date().getTime() / 1000);
    let date = new Date(publishTime * 1000);
    let Y = date.getFullYear();
    let M = date.getMonth() + 1;
    let D = date.getDate();
    let H = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    if (M < 10) {
        M = '0' + M;
    }
    if (D < 10) {
        D = '0' + D;
    }
    if (H < 10) {
        H = '0' + H;
    }
    if (m < 10) {
        m = '0' + m;
    }
    if (s < 10) {
        s = '0' + s;
    }
    d = nowTime - publishTime;
    dDays = parseInt(d / 86400);
    if (dDays > 0) {
        return `${Y}-${M}-${D}`;
    } else {
        return `${H}:${m}:${s}`;
    }
});

Vue.filter('floatZero', (t) => {
    let value = Math.round(parseFloat(t) * 100) / 100;
    let point = value.toString().split(".");
    if (point.length === 1) {
        value = value.toString() + ".00";
        return value;
    }
    if (point.length > 1) {
        if (point[1].length < 2) {
            value = value.toString() + "0";
        }
        return value;
    }
});

Vue.filter('photoFilter', (t) => {
    console.log(t)
    let newPhoto=[];
    t.forEach(function (element, index) {
        console.log(element); // red green blue
        console.log(index);   // 0 1 2
    });
    return newPhoto;
});

Vue.filter('distanceFilter', (d) => {
    let newDistance='';
    if(d<100){
        newDistance='<100m';
    }else if(d>=100&&d<=1000){
        newDistance=d+'m';
    }else if(d>=100&&d<=1000000){
        newDistance=(d/1000).toFixed(1)+'km';
    }else{
        newDistance='>1000km';
    }
    return newDistance;
});

Vue.filter('msgAbbreviation', (str) => {
    if(str.indexOf('img[')>=0){
        return '[图片消息]'
    }

    return str;
});
